import os
import math
import numpy as np
from netCDF4 import Dataset
import csv
from wrf import (to_np, getvar, smooth2d, get_cartopy, cartopy_xlim,
                 cartopy_ylim, latlon_coords)



mainpath = '/*********/'
Hurricaneall = ['Dorian']
gridsize = ['8km']
Dirall = ['_isftcflx_1_changeClz_0p0001',
       '_isftcflx_1_changeClz_0p0100',
       '_isftcflx_1_changeClz_1p0000',
       '_isftcflx_1_changeClz_100p0000']
outputpath = '/*******************/'
Real_Hurricane_Data = ['Dorian_Real_Track_Time_NOAA.csv']



# This function returns a list of all wrf files in the directory.
def list_files(Dir, ncfiles):
        for f in os.listdir(Dir):
            if f.startswith('wrfout'):
                ncfiles.append(f)
        return ncfiles
    


for grids in gridsize:
    count1=0
    for Hurricane in Hurricaneall:
        rows=[]
        
    
        
        for Dir in Dirall:

            print('Current folder is: ')
            Dir_local = mainpath+Hurricane+ '/' +grids+ '/' +'WRFONLY_NoTurb_'+grids+Dir
            print(Dir_local)
            
            # Set the working space>
            os.chdir(Dir_local)
            # initiate the list that will contain all wrf files in Dir directory.
            ncfiles = []
            # Use the list_files function to list all the wrf files in the directory.
            ncfiles = list_files(Dir_local, ncfiles)
            ncfiles = sorted(ncfiles)
            # initiate the list that will contain the hurricane-track data.
            row = []
            # Identify the time step
            Time_Step = 6
            k = 0
            # initiate the list that will contain the times.
            Times = []
            for tt in range(1):
                for ncfile in ncfiles:
                    ncfile = Dataset(ncfile)
                    ttt = np.array(getvar(ncfile, "times", tt))
                    # Get U and V components of wind intensity at 10m of altitude.
                    U10_2D = np.array(getvar(ncfile, "U10", tt))
                    V10_2D = np.array(getvar(ncfile, "V10", tt))
                    slp_2D = np.array(getvar(ncfile, "slp", tt))
                    slp_2D = slp_2D.flatten()
                    # Reshape the U and V into a 1D array.
                    U10_1D = U10_2D.flatten()
                    V10_1D = V10_2D.flatten()
                    WND_SPD_10 = U10_1D
                    # Calculate the wind intensity at each point of the map.
                    for i in range (WND_SPD_10.size - 1):
                                    WND_SPD_10[i] = math.sqrt((U10_1D[i]**2)+(V10_1D[i]**2))
                    # Search for the maximum wind intensity at aspecific time step.	
                    WND_SPD_10_max = np.amax(WND_SPD_10)
                    slp_min = np.amin(slp_2D)	
                    # List the maximum wind intensity for all time steps.	
                    row.append(WND_SPD_10_max)
                    # list all the time steps
                    Times.append(Time_Step*k)
                    k = k+1 

            rows.append(row)
        fields = [time for time in Times]

        with open(outputpath+Hurricane+'_wind_intensity_'+grids+'.csv', 'w') as csvfile:
            csvwriter = csv.writer(csvfile)
            csvwriter.writerow(fields) 
            csvwriter.writerows(rows)
    
        count1=count1+1







    
